package cc.yugu.yhdw.webserver.service.hdw;

import cc.yugu.yhdw.common.domain.basic.CustomerInOutMoney;
import cc.yugu.yhdw.common.domain.basic.Passport;
import cc.yugu.yhdw.common.domain.hdw.ExchangePacketOrderRefund;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.basic.CustomerInOutMoneyMapper;
import cc.yugu.yhdw.webserver.persistence.basic.PassportMapper;
import cc.yugu.yhdw.webserver.persistence.hdw.ExchangePacketOrderRefundMapper;
import cc.yugu.yhdw.webserver.service.AbstractService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
/**
 * 换电套餐订单退款业务
 * 
 * @Date:Created in 2018/7/9 13:40
 * @Author:
 * @Modified By: zjn 2018/7/9 13:40
 */ 
@Service
public class ExchangePacketOrderRefundService extends AbstractService {

    @Autowired
    ExchangePacketOrderRefundMapper exchangePacketOrderRefundMapper;
    @Autowired
    PassportMapper passportMapper;
    @Autowired
    CustomerInOutMoneyMapper customerInOutMoneyMapper;
    /**
    * 分页查询
    * 
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/9  13:40
    */ 
    public Page findPage(ExchangePacketOrderRefund search) {
        Page page = search.buildPage();
        page.setTotalItems(exchangePacketOrderRefundMapper.findPageCounts(search));
        search.setBeginIndex(page.getOffset());
        List<ExchangePacketOrderRefund> list = exchangePacketOrderRefundMapper.findPageResults(search);
        // 循环设置代理商和运营商名称（非常低效率）--推荐多表联查
        for (ExchangePacketOrderRefund packetPeriodOrderRefund: list) {
            Integer agentId = packetPeriodOrderRefund.getAgentId();
            Integer agencyId = packetPeriodOrderRefund.getAgencyId();
            // 设置运营商名称
            if (agentId != null) {
                if (findAgentInfo(agentId) != null) {
                    // 非常耗费时间
                    packetPeriodOrderRefund.setAgentName(findAgentInfo(agentId).getAgentName());
                }
            }
            // 设置代理商名称
            if (agencyId != null) {
                if (findAgency(agencyId) != null) {
                    packetPeriodOrderRefund.setAgencyName(findAgency(agencyId).getAgencyName());
                }
            }
        }
        page.setResult(list);
        return page;
    }

    public ExchangePacketOrderRefund find(String id) {
        return exchangePacketOrderRefundMapper.find(id);
    }
    /**
    * 退款处理
    * 
    * @Param: [userName, packetPeriodOrderRefund]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  13:41
    */ 
    @Transactional(rollbackFor=Throwable.class)
    public ExtResult refund(String userName, ExchangePacketOrderRefund packetPeriodOrderRefund) {
        ExchangePacketOrderRefund entity = exchangePacketOrderRefundMapper.find(packetPeriodOrderRefund.getId());
        if (entity == null) {
            return ExtResult.failResult("订单不存在");
        }
        if (entity.getMoney() == null) {
            return ExtResult.failResult("退款失败, 余额为0");
        }
        if (entity.getRefundStatus() != ExchangePacketOrderRefund.RefundStatus.APPLY_REFUND.getValue() ) {
            return ExtResult.failResult("只有申请退款状态才可退款！");
        }
        if (packetPeriodOrderRefund.getRefundMoney() > entity.getMoney()){
            return ExtResult.failResult("退款金额不能大于订单金额");
        }
        if(StringUtils.isEmpty(packetPeriodOrderRefund.getRefundReason())){
            return ExtResult.failResult("退款原因不能为空");
        }

        CustomerInOutMoney money = new CustomerInOutMoney();
        money.setCustomerId(entity.getCustomerId());
        money.setBizType(CustomerInOutMoney.BizType.CUSTOMER_EXCHANGE_PACKET_ORDER_REFUND.getValue());
        money.setSourceId(entity.getId());
        money.setMoney(packetPeriodOrderRefund.getRefundMoney());
        money.setCreateTime(new Date());
        customerInOutMoneyMapper.insert(money);

        Passport customer = passportMapper.find(entity.getCustomerId());
        if (customer == null) {
            return ExtResult.failResult("客户不存在");
        }

        int total = passportMapper.updateBalance(customer.getId(), packetPeriodOrderRefund.getRefundMoney());
        if (total == 0) {
            return ExtResult.failResult("退款失败");
        }
        exchangePacketOrderRefundMapper.updateStatus(packetPeriodOrderRefund.getId(),
                ExchangePacketOrderRefund.RefundStatus.REFUND_SUCCESS.getValue(),
                packetPeriodOrderRefund.getRefundMoney(), userName, new Date(),
                packetPeriodOrderRefund.getRefundReason());
        return ExtResult.successResult();
    }
    /**
    * 拒绝退款
    * 
    * @Param: [userName, packetPeriodOrderRefund]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/9  13:41
    */ 
    public ExtResult repulseRefund(String userName, ExchangePacketOrderRefund packetPeriodOrderRefund) {
        // 设置退款状态为拒绝退款
        packetPeriodOrderRefund.setRefundStatus(ExchangePacketOrderRefund.RefundStatus.REFUSE_REFUND.getValue());
        // 设置退款人姓名
        packetPeriodOrderRefund.setRefundOperator(userName);
        int total = exchangePacketOrderRefundMapper.update(packetPeriodOrderRefund);
        if (total == 0) {
            return ExtResult.failResult("操作失败");
        }
        return ExtResult.successResult();
    }

}
